package com.lelefly.base.daoimp;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.lelefly.base.model.SearchInsuranceBean;
import com.lelefly.base.po.LeleflyCompanyInsurance;
import com.lelefly.db.dbmanager.HibernateSessionFactory;

@Repository
public class InsuranceDao extends BaseDao {

	public List getInsuranceInfo(String sql) {
		SessionFactory sessionFactory = HibernateSessionFactory
				.getSessionFactory();
		Session session = sessionFactory.openSession();
		try {
			List sqlQuery = session
					.createSQLQuery(sql)
					.addScalar("insuranceNo")
					.addScalar("beginDate")
					.addScalar("endDate")
					.addScalar("status")
					.addScalar("type")
					.setResultTransformer(
							Transformers.aliasToBean(SearchInsuranceBean.class))
					.list();
			return sqlQuery;
		} catch (Exception e) {
			// TODO: handle exception
			getLogger().info("----->>>" + e.getMessage());
			return null;
		} finally {
			session.close();
		}
	}

	public List<LeleflyCompanyInsurance> queryAllInsurance(String hql,
			int page, int count) {

		SessionFactory sessionFactory = HibernateSessionFactory
				.getSessionFactory();
		Session session = sessionFactory.openSession();

		try {
//			Query query = session.createSQLQuery(hql).addScalar("id")
//					.addScalar("insuranceNumber")
//					.addScalar("companyname")
//					.addScalar("socialCreditCode")
//					.addScalar("username")
//					.addScalar("phonenumber")
//					.addScalar("idno")
//					.addScalar("email")
//					.addScalar("beginTime")
//					.addScalar("endTime")
//					.addScalar("flyerName")
//					.addScalar("flyerIdNo")
//					.addScalar("addDate")
//					.addScalar("insuranceState")
//					.addScalar("planeId")
//					.addScalar("planeName")
//					.setResultTransformer(
//							Transformers.aliasToBean(LeleflyCompanyInsurance.class));
			Query query = session.createSQLQuery(hql).addEntity(LeleflyCompanyInsurance.class);
//					.addScalar("id")
//					.addScalar("insurance_number")
//					.addScalar("companyname")
//					.addScalar("social_credit_code")
//					.addScalar("username")
//					.addScalar("phonenumber")
//					.addScalar("idno")
//					.addScalar("email")
//					.addScalar("begin_time")
//					.addScalar("end_time")
//					.addScalar("flyer_name")
//					.addScalar("flyer_id_no")
//					.addScalar("add_date")
//					.addScalar("insurance_state")
//					.addScalar("plane_id")
//					.addScalar("plane_name")
//					.setResultTransformer(Transformers.aliasToBean(LeleflyCompanyInsurance.class));

			query.setFirstResult(page);
			query.setMaxResults(count);

			return query.list();
		} catch (Exception e) {
			return null;
		} finally {
			session.close();
		}
	}
}
